package presenters;

import java.sql.ResultSet;
import java.sql.SQLException;

import model.ConnectionManager;
import model.DbSchema;
import model.IPresenter;
import model.Model;
import views.MaintReqPanel;

public class MaintReqPresenter extends IPresenter {
	private MaintReqPanel myPanel;
	
	public MaintReqPresenter(MaintReqPanel p) {
		myPanel = p;
	}
	
	public void submitRequest(String location, String car, String description) {
		if(checkString2(location) && checkString(car) && checkString2(description)) {
						
			/*
			String sql = "INSERT INTO " + ConnectionManager.GROUP
					+ "." + DbSchema.SRP_TABLE + "("
					+ DbSchema.SRP_VIN + ", "
					+ DbSchema.SRP_DTTM + ", "
					+ DbSchema.SRP_DESC + ") VALUES('"
						+ vin 			+ "', '"
						+ date 			+ "', '"
						+ description 	+ "');"; */
			String sqlVin = "(SELECT VIN FROM " + ConnectionManager.GROUP + ".CAR WHERE MDL_NM = '" + car + "' AND LOC = '" + location + "');";
			ResultSet rsVin = executeSQLQuery(sqlVin);
			
			String MtcVIN = "";
			try {
				rsVin.first();
				MtcVIN = rsVin.getString("VIN");
			} catch(SQLException e) {
				e.printStackTrace();
			}
			


			//Parse the text field by bullet numbers (or create distinct fields for each problem and loop through entering each one into the table)

//			for (String problem : problemsArray) {
//				String sqlLine = "INSERT INTO SERVICE_REQUEST_PROBLEMS VALUES(MtcVIN, CURRENT_TIMESTAMP, problem);";
//			}
			String username = Model.getCurrentUser();
			String sqlIn = "INSERT INTO SERVICE_REQUEST_PROBLEMS(RQST_VIN, RQST_DTTM, PROBLEM_DISC) VALUES('"
				+ MtcVIN + "', CURRENT_TIMESTAMP, '" + username + "');";
			
			int out = executeSQLUpdate(sqlIn);
			System.out.println("Request added to row: " + out);
		}
	}
}
